package com.haiyi.software.app.basic;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.Header;

import android.R.integer;
import android.location.Location;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.haiyi.software.app.MainApplication;
import com.haiyi.software.app.R;
import com.haiyi.software.entity.UserInfo;
import com.haiyi.software.tools.MyTools;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

public abstract class FragmentBasicNearby extends FragmentBasic {

	protected abstract void requestError();

	private final String TAG = "FragmentBasicNearby";

	protected List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

	protected List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();

	public String str_requestURL = null;

	// 默认头像
	public String str_defaultPhoto = null;

	private final String str_requestName = "nearPerson.php";

	private Boolean isWork = false;

	protected String str_appearTime = "99999999";

	protected String str_sex = "-1";

	protected String str_ageMax = "200";

	protected String str_ageMin = "18";

	protected int page = 1;

	protected int pagetotal = -1;

	public Boolean getIsWork() {

		return isWork;
	}

	@Override
	public synchronized void requestNetworkData() {

		str_requestURL = this.getResources().getString(R.string.URL);
		str_defaultPhoto = this.getResources().getString(R.string.DefaultPhoto);
		str_requestURL = str_requestURL + str_requestName;

		AsyncHttpClient client = new AsyncHttpClient();
		RequestParams params = new RequestParams();
		params.add("args", getJsonFromObject());

		client.post(str_requestURL, params, responseHandler);

	}

	private AsyncHttpResponseHandler responseHandler = new AsyncHttpResponseHandler() {

		@Override
		public void onSuccess(int statusCode, Header[] headers,
				byte[] responseBody) {
			// TODO 自动生成的方法存根
			try {
				String str_jsonReturn = new String(responseBody, "UTF-8");

				Gson gson = new Gson();

				JsonParser parser = new JsonParser();

				JsonObject jsObj = parser.parse(str_jsonReturn)
						.getAsJsonObject();

				JsonArray jsonArray = jsObj.getAsJsonObject("body")
						.getAsJsonArray("personlist");
				Type type = new TypeToken<Map<String, String>>() {
				}.getType();

				for (int i = 0; i < jsonArray.size(); i++) {
					JsonElement el = jsonArray.get(i);

					Map<String, Object> tmp = gson.fromJson(el, type);
					list.add(tmp);
				}

				data.clear();
				data.addAll(list);

				pagetotal = jsObj.getAsJsonObject("body").get("pagetotal")
						.getAsInt();

				requestFinish();

			} catch (Exception e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}
		}

		@Override
		public void onFailure(int statusCode, Header[] headers,
				byte[] responseBody, Throwable error) {

			requestError();
		}

	};

	private String getJsonFromObject() {

		UserInfo myInfo = MainApplication.getInstance().getMineUserInfo();
		if (myInfo != null) {

			Map<String, Object> validator = new HashMap<String, Object>();

			validator.put("uid", myInfo.getUid());
			validator.put("uname", myInfo.getUname());
			validator.put("pwd", myInfo.getPwd());
			validator.put("uguid", myInfo.getUguid());

			Map<String, Object> body = new HashMap<String, Object>();

			Location loc = new MyTools().getLocation();
			if (loc != null) {
				body.put("lat", loc.getLatitude());
				body.put("longitude", loc.getLongitude());

			} else {
				body.put("lat", 0);
				body.put("longitude", 0);
			}

			body.put("usex", str_sex);
			body.put("uwork", "");
			body.put("uedu", "");
			body.put("page", page);
			body.put("percount", 999999);

			body.put("qtime", str_appearTime);

			Map<String, Object> age = new HashMap<String, Object>();
			age.put("min", str_ageMin);
			age.put("max", str_ageMax);
			body.put("age", age);

			Map<String, Object> umoney = new HashMap<String, Object>();
			umoney.put("min", 0);
			umoney.put("max", 9999999);
			body.put("umoney", umoney);

			Map<String, Object> args = new HashMap<String, Object>();
			args.put("validator", validator);
			args.put("body", body);
			Gson gson = new Gson();
			String s = gson.toJson(args);

			return s;
		} else {
			return null;
		}

	}
}
